/**
 * @author ChaP
 * @date 2019/06/28
 * @describe:
 */
package CodingTest.AC20190628;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class findMode {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x){val = x;}
    }
    HashMap<Integer,Integer> map = new HashMap<>();
    int max =Integer.MIN_VALUE;
   public void dfs(TreeNode root){
       if(root == null) return ;
        else{
            if(map.containsKey(root.val)) {
                if (max <= map.get(root.val))
                    max = map.get(root.val) + 1;
                map.put(root.val, map.get(root.val) + 1);
            }else{
                if(max <1){
                    max = 1;
                }
                map.put(root.val,1);
            }
            dfs(root.left);
            dfs(root.right);
        }
   }
   public int[] findMode(TreeNode root){
       HashMap<Integer,Integer> hs = new HashMap<>();
       List<Integer> list = new ArrayList<>();
       for(Integer e :hs.keySet()){
           if(hs.get(e) == max){
               list.add(hs.get(e));
           }
       }
       int[] result = new int[list.size()];
       for(int i = 0;i<list.size();i++){
           result[i] = list.get(i);
       }
       return result;
   }
}
